package com.ljh.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljh.blog.common.constant.Constants;
import com.ljh.blog.common.exception.BlogException;
import com.ljh.blog.common.utils.BeanTransUtils;
import com.ljh.blog.mapper.BlogInfoMapper;
import com.ljh.blog.pojo.dataobject.BlogInfo;
import com.ljh.blog.pojo.request.AddBlogRequest;
import com.ljh.blog.pojo.request.UpdateBlogRequest;
import com.ljh.blog.pojo.response.BlogInfoResponse;
import com.ljh.blog.service.BlogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Themberfue
 * @date: 2025/3/30 20:50
 * @description:
 */
@Service
public class BlogServiceImpl implements BlogService {
    private static final Logger log = LoggerFactory.getLogger(BlogServiceImpl.class);
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, 0);
        return blogInfoMapper.selectList(queryWrapper).stream()
                .map(BeanTransUtils::trans)
                .collect(Collectors.toList());
    }

    @Override
    public BlogInfoResponse getBlogDetails(Integer blogId) {
        return BeanTransUtils.trans(getBlogInfo(blogId));
    }

    @Override
    public BlogInfo getBlogInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, 0)
                .eq(BlogInfo::getId, blogId);
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean postBlog(AddBlogRequest blog) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(blog, blogInfo);
        try {
            int insert = blogInfoMapper.insert(blogInfo);
            if (insert < 1) {
                return false;
            }
        } catch (Exception e) {
            log.error("上传博客失败");
            throw new BlogException("内部错误，请联系管理员");
        }
        return true;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = getBlogInfo(blogId);
        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }
        try {
            blogInfo.setDeleteFlag(Constants.BLOG_DELETE);
            int res = blogInfoMapper.updateById(blogInfo);
            return res == 1;
        } catch (Exception e) {
            log.error("删除博客失败...");
            throw new BlogException("内部错误，请联系管理员");
        }
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = BeanTransUtils.trans(updateBlogRequest);
        if (blogInfo == null) {
            throw new BlogException("博客不存在");
        }
        try {
            int res = blogInfoMapper.updateById(blogInfo);
            return res == 1;
        } catch (Exception e) {
            log.error("更新博客失败");
            throw new BlogException("内部错误，请联系管理员");
        }
    }
}
